package com.njworkorder.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njworkorder.DTO.ConstructionIndexItemsAddDto;
import com.njworkorder.Entity.ConstructionIndexItems;
import com.njworkorder.Entity.ConstructionIndicators;
import com.njworkorder.Service.ConstructionIndexItemsService;
import com.njworkorder.Service.ConstructionIndicatorsService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import com.njworkorder.VO.CIIListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

@Tag(name = "施工指标类与项", description = "施工指标类与项相关接口")
@RestController
@RequestMapping("/construction")
public class ConstructionController {

    private final ConstructionIndicatorsService constructionIndicatorsService;
    private final ConstructionIndexItemsService constructionIndexItemsService;
    public ConstructionController(ConstructionIndicatorsService constructionIndicatorsService, ConstructionIndexItemsService constructionIndexItemsService) {
        this.constructionIndicatorsService = constructionIndicatorsService;
        this.constructionIndexItemsService = constructionIndexItemsService;
    }

    @Operation(summary = "添加施工指标类信息")
    @PostMapping("/addCIInfo")
    public Result<String> addCIInfo(@Schema(description = "指标类名称") @RequestParam("indicatorsName") String indicatorsName,
                                    @Schema(description = "备注") @RequestParam("remark") String remark,
                                    @Schema(description = "状态") @RequestParam("status") String status,
                                    @Schema(description = "病害模块") @RequestParam("diseaseModule") String diseaseModule){
        ConstructionIndicators constructionIndicators = new ConstructionIndicators();
        constructionIndicators.setDiseaseModule(diseaseModule);
        constructionIndicators.setIndicatorsName(indicatorsName);
        constructionIndicators.setRemark(remark);
        constructionIndicators.setStatus(status);

        boolean save = constructionIndicatorsService.save(constructionIndicators);
        if (save){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.error("添加失败");
    }

    @Operation(summary = "根据id修改施工指标类信息")
    @Parameter(name = "indicatorsName", description = "指标类名称", required = true)
    @PostMapping("/updateCIInfoById")
    public Result<String> updateCIInfoById(@RequestParam("id") String id,
                                           @Schema(description = "指标类名称") @RequestParam("indicatorsName") String indicatorsName,
                                           @Schema(description = "备注") @RequestParam("remark") String remark,
                                           @Schema(description = "状态") @RequestParam("status") String status,
                                           @Schema(description = "病害模块") @RequestParam("diseaseModule") String diseaseModule){

        ConstructionIndicators constructionIndicators = new ConstructionIndicators();
        constructionIndicators.setId(id);
        constructionIndicators.setIndicatorsName(indicatorsName);
        constructionIndicators.setDiseaseModule(diseaseModule);
        constructionIndicators.setRemark(remark);
        constructionIndicators.setStatus(status);

        boolean updateById = constructionIndicatorsService.updateById(constructionIndicators);
        if (updateById){
            return ResultUtil.success("更新成功");
        }
        return ResultUtil.error("更新失败");
    }


    @Operation(summary = "根据id获取施工指标类信息")
    @GetMapping("/getCIById")
    public Result<ConstructionIndicators> getCIInfoById(@RequestParam("id") String id){
        ConstructionIndicators constructionIndicators = constructionIndicatorsService.getById(id);
        return ResultUtil.success(constructionIndicators);
    }

    @Operation(summary = "根据id删除施工指标类信息")
    @GetMapping("/deleteCIById")
    public Result<String> deleteCIById(@RequestParam("id") String id){
        boolean removeById = constructionIndicatorsService.removeById(id);
        if (removeById){
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.error("删除失败");
    }

    @Operation(summary = "获取施工指标类列表")
    @GetMapping("/getCIList")
    public Result<Page<ConstructionIndicators>> getCIList(@RequestParam("pageIndex") int pageIndex,
                                                          @RequestParam("pageSize") int pageSize) {
        LambdaQueryWrapper<ConstructionIndicators> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.apply("1=1");

        Page<ConstructionIndicators> page = constructionIndicatorsService.page(new Page<>(pageIndex, pageSize), queryWrapper);

        return ResultUtil.success(page);
    }



    @Operation(summary = "添加施工指标项信息")
    @PostMapping("/addCIIInfo")
    public Result<String> addCIIInfo(@RequestBody ConstructionIndexItemsAddDto constructionIndexItemsAddDto){
        ConstructionIndexItems constructionIndexItems = new ConstructionIndexItems();

        BeanUtils.copyProperties(constructionIndexItemsAddDto,constructionIndexItems);

        boolean save = constructionIndexItemsService.save(constructionIndexItems);
        if (save){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.error("添加失败");
    }

    @Operation(summary = "根据id修改施工指标项记录")
    @PostMapping("/updateCIInfoByIds/{id}")
    public Result<String> updateCIInfoByIds(@RequestBody ConstructionIndexItemsAddDto constructionIndexItemsAddDto,
                                            @PathVariable("id") String id){
        ConstructionIndexItems constructionIndexItems = new ConstructionIndexItems();
        BeanUtils.copyProperties(constructionIndexItemsAddDto,constructionIndexItems);
        constructionIndexItems.setId(id);
        boolean save = constructionIndexItemsService.updateById(constructionIndexItems);
        if (save){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.error("添加失败");
    }

    @Parameter(name = "constructionIndicatorsId", description = "施工指标类id")
    @Operation(summary = "获取施工指标项列表")
    @GetMapping("/getCIIList")
    public Result<Page<CIIListVo>> getCIIList(@RequestParam("pageIndex") int pageIndex,
                                              @RequestParam("pageSize") int pageSize,
                                              @RequestParam("constructionIndicatorsId") String constructionIndicatorsId){

        MPJLambdaWrapper<ConstructionIndexItems> mpjLambdaWrapper = new MPJLambdaWrapper<ConstructionIndexItems>()
                .selectAll(ConstructionIndexItems.class)
                .leftJoin(ConstructionIndicators.class, ConstructionIndicators::getId, ConstructionIndexItems::getConstructionIndicatorsId,ext ->
                        ext.selectAs(ConstructionIndicators::getIndicatorsName,CIIListVo::getIndicatorsName))
                .apply("1=1").eq(!constructionIndicatorsId.isEmpty(), ConstructionIndexItems::getConstructionIndicatorsId, constructionIndicatorsId);

        Page<CIIListVo> ciiListVoPage = constructionIndexItemsService.selectJoinListPage(new Page<>(pageIndex, pageSize), CIIListVo.class, mpjLambdaWrapper);

        return ResultUtil.success(ciiListVoPage);
    }

    @Operation(summary = "根据id删除施工指标项信息")
    @GetMapping("/deleteCIIById")
    public Result<String> deleteCIIById(@RequestParam("id") String id){
        boolean removeById = constructionIndexItemsService.removeById(id);
        if (removeById){
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.error("删除失败");
    }

    @Operation(summary = "根据id获取施工指标项信息")
    @GetMapping("/getCIIById")
    public Result<ConstructionIndexItems> getCIIById(@RequestParam("id") String id){
        ConstructionIndexItems constructionIndexItems = constructionIndexItemsService.getById(id);
        return ResultUtil.success(constructionIndexItems);
    }

}
